<template>
  <div id="app">
    <div>
      <span>姓名:</span>
      <input type="text" v-model="uersName"/>
    </div>
    <div>
      <span>年龄:</span>
      <input type="number" v-model="age"/>
    </div>
    <div>
      <span>性别:</span>
      <select v-model="sex">
        <option value="男">男</option>
        <option value="女">女</option>
      </select>
    </div>
    <div>
      <button @click="addFn">添加/修改</button>
    </div>
    <div>
      <table
        border="1"
        cellpadding="10"
        cellspacing="0"
      >
        <tr>
          <th>序号</th>
          <th>姓名</th>
          <th>年龄</th>
          <th>性别</th>
          <th>操作</th>
        </tr>
        <tr v-for="(obj,index) in list" :key="index">
          <td>{{index+1}}</td>
          <td>{{obj.name}}</td>
          <td>{{obj.age}}</td>
          <td>{{obj.sex}}</td>
          <td>
            <button @click="delFn(index)">删除</button>
            <button @click="modFn(obj,index)">编辑</button>
          </td>
        </tr>
      </table>
    </div>
  </div>
</template>
<script>
export default {
  data () {
    return {
      age:'',
      uersName:'',
      sex:'',
      ind:-1,
      list:[{
        name:"诺克",
        age:"19",
        sex:"男",
      },
      {
        name:"艾莉",
        age:"15",
        sex:"女",
      }]
    }
  },
  methods:{
    addFn(){
     if(this.ind==-1){
        this.list.push({
        name:this.uersName,
        age:this.age,
        sex:this.sex
        })
     }else{
      this.$set(this.list,this.ind,
      {name:this.uersName,
        age:this.age,
        sex:this.sex});
      this.ind=-1
     }
        
     
    },
    delFn(index){
      this.list.splice(index,1)
    },
    modFn(obj,index){
      this.uersName=obj.name
      this.age=obj.age
      this.sex=obj.sex
      this.ind=index
    }
  }
}
</script>
